package com.example.cofeed.adapter;

import android.content.Intent;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cofeed.R;
import com.example.cofeed.activity.DetailsActivity;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.Msg;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.text.SimpleDateFormat;

public class NoteAdapter extends BaseQuickAdapter<Msg, BaseViewHolder> {
    public NoteAdapter() {
        super(R.layout.adapter_note);
    }

    @Override
    protected void convert(BaseViewHolder helper, Msg item) {

        int headingFontSize = SPUtils.getInstance().getInt(SputilsConstant.HEADING_FONT_SIZE);
        TextView titleTv = (TextView) helper.getView(R.id.tv_title);
        TextView timeTv = (TextView) helper.getView(R.id.tv_date);

//        switch (headingFontSize){
//            case 0:
//                titleTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size1)));
//                timeTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size1)));
//                break;
//            case 1:
//                titleTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size2)));
//                timeTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size2)));
//                break;
//            case 2:
//                titleTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size3)));
//                timeTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size3)));
//                break;
//            case 3:
//                titleTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size4)));
//                timeTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size4)));
//                break;
//            case 4:
//                titleTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size5)));
//                timeTv.setTextSize(SizeUtils.px2sp(mContext.getResources().getDimension(R.dimen.text_font_size5)));
//                break;
//        }
        boolean today = TimeUtils.isToday(item.createtime);
        String createtime = "";
//        if (isAllShow){
//            if (today){
//                createtime = TimeUtils.date2String(TimeUtils.string2Date(item.createtime), new SimpleDateFormat("HH:mm"));
//            }else {
//                createtime = TimeUtils.date2String(TimeUtils.string2Date(item.createtime), new SimpleDateFormat("MM-dd"));
//            }
//        }else
//            createtime = TimeUtils.date2String(TimeUtils.string2Date(item.createtime), new SimpleDateFormat("HH:mm"));

        if (today){
            createtime = TimeUtils.date2String(TimeUtils.string2Date(item.createtime), new SimpleDateFormat("HH:mm"));
        }else {
            createtime = TimeUtils.date2String(TimeUtils.string2Date(item.createtime), new SimpleDateFormat("MM-dd"));
        }

        if (!ObjectUtils.isEmpty(item.signlist)){
            StringBuffer stringBuffer = new StringBuffer();
            for (String s:item.signlist) {
                stringBuffer.append(s).append("/");
            }
            String desc = "";
            if (!ObjectUtils.isEmpty(stringBuffer.toString())){
                desc = stringBuffer.toString().substring(0, stringBuffer.length() -1);
            }else {
                desc = stringBuffer.toString();
            }
            helper.setText(R.id.tv_title, desc);
        }else {
            helper.setText(R.id.tv_title, "");
        }
        helper.setText(R.id.tv_date, createtime);

        final TextView textview = (TextView) helper.getView(R.id.tv_content);
        String content = item.content;
        textview.setText(content.trim());
        textview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //一般用完之后，立即移除该监听
                textview.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                TextPaint paint = textview.getPaint();

                int paddingLeft = textview.getPaddingLeft();
                int paddingRight = textview.getPaddingRight();

                //给省略号留的长度（但是，因为字符占位问题，获取的这个长度，要比省略号的三个点的长度大一些）
                float moreText = textview.getTextSize() * 3;

                //乘2，是代表2行的意思，减去moreText，是给省略号预留一点位置
                float availableTextWidth = (textview.getWidth() - paddingLeft - paddingRight) * 10 - moreText;
                /**
                 * TextUtils中public static CharSequence ellipsize(CharSequence text,TextPaint p,float avail, TruncateAt where)说明
                 *
                 * Returns the original（原始） text if it fits（适合、符合） in the specified（指定） width
                 * given the properties（性质） of the specified（指定） Paint,
                 * or, if it does not fit, a truncated（缩短了的，被删截的）
                 * copy with ellipsis（省略、省略符号） character added at the specified（指定） edge（边缘） or center.
                 */
                CharSequence ellipsizeStr = TextUtils.ellipsize(textview.getText().toString(), paint, availableTextWidth, TextUtils.TruncateAt.END);


                textview.setText(ellipsizeStr);

            }
        });
//        helper.setText(R.id.tv_content, item.content+item.content+item.content+item.content);

        helper.addOnClickListener(R.id.layout_cv);

//        helper.getView(R.id.layout_cv).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                Msg msg = (Msg) adapter.getData().get(position);
//                Intent intent = new Intent(mContext, DetailsActivity.class);
//                intent.putExtra(SputilsConstant.MSG_ID, item.smsid);
//                mContext.startActivity(intent);
//            }
//        });

    }
}
